
<?php

class yewuAction extends SaleAction
{
    public function index()
    {
        $projectdata=$this->getmyProject();
        $this->list = $projectdata;
        $this->display();
    }



    public function add()
    {
        $cust_model = M("custom");
        $uid = I("get.uid");

        $cust_info = $cust_model->find($uid);
        if (!$cust_info) $this->error("没有找到用户");
        $this->custinfo = $cust_info;

        $projecttype = M("projecttype");
        $this->list = $projecttype->select();
        $this->display();
    }

    // 检查标题是否可用
    public function checkphone()
    {

        /*  dump($_POST);*/
        $telphone = I("post.title", "");
        $telphone = trim($telphone);
        if (!empty($telphone))
        {
            $Form = M("customer");
            if ($Form->getBytelephone($telphone))
            {
                $this->error('电话号已经存在');
            } else
            {
                $this->success('电话号可以使用!');
            }
        } else
        {
            $this->error('电话号必须');
        }
    }

    public function  validate()
    {
        if (!isset($_POST))
        {
            exit;
        }

        $project = new projectModel();
        $ruler = $project->getProperty("_validate");


        $newrule = array();
        foreach ($_POST as $k => $v)
        {
            foreach ($ruler as $kr => $vr)
            {
                if ($vr[0] == $k)
                {
                    $newrule[] = $vr;
                }
            }
        }
        $project->setProperty("_validate", $newrule);
        dump($_POST);
        dump($newrule);

        $valiinfo = $project->create();
        if (!$valiinfo)
        {
            dump($valiinfo);
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            echo $project->getError();
            dump("it is error");
        }
        else
        {

            echo "good";
        }




        //echo "exit";exit;


    }

    public function addproject()
    {


        $project = D('project');
        $projectdata = $project->create();
        if (!$projectdata) $this->error($project->getError());



        $userkeyword = $projectdata["keyword"];
        $userkeyword = str_replace(" ", "", $userkeyword);
        $projectdata["keyword"] = $userkeyword;
        $projectdata["employee"] = I("session.userid");
        $projectdata["createtime"] = date("Y-m-d H:i:s");


        $customer = M('custom');
        $custid = $projectdata["custid"];
        $companyname = $customer->where("id=" . $custid)->getField("company");

        $projtype = $projectdata["type"];
        $projecttype = $this->CacheArray("projecttype");
        $projectdata["name"] = $companyname . '-' . $projectdata["keyword"] . '-' . $projecttype[$projtype]["name"];


        $content = trim($projectdata["content"]);
        $content = htmlspecialchars($content);
        if (get_magic_quotes_gpc()) $content = stripslashes($content);
        $projectdata["content"] = $content;

        $projectid = $project->add($projectdata);
        if ($projectid)
        {
            $jumpurl = U("yewu/index");
            $this->assign("jumpUrl", $jumpurl);
            $this->success("添加成功");
        } else
        {
            $this->error("添加失败");
        }

    }

    public function yewudel()
    {
        $project = M('project');
        $id = I('get.id'); // $_GET['id'];
        if (empty($id)) $this->error("参数错误");
        $data = array();
        $data["id"] = $id;
        $data["isdelete"] = 1;
        //$sql="update ys_project set isdelete=1 where id=$id";
        $res = $project->save($data);
        if ($res > 0)
        {
            $this->success("删除成功");
        } else
        {
            $this->error("删除失败");
        }
    }

    public function yewuupdate()
    {
        $projecttype = M('projecttype');
        $this->list = $projecttype->select();
        $id = I("get.id");
        $arr = $this->getProjectInfo($id);
        $this->assign('data', $arr);
        $this->display();
    }
    public function yewuContent()
    {
        $projecttype = M('projecttype');
        $this->list = $projecttype->select();
        $id = I('get.id');
        $arr=$this->getProjectInfo($id);
        $this->assign('data', $arr);
        $this->display();
    }

    public function yewuupdatesave()
    {
        $project = D('project');
        $pro = $project->create();
        if (!$pro) $this->error($project->getError());
        $content = I('post.content');

        $content = htmlspecialchars($content);
        if (get_magic_quotes_gpc())
        {
            $content = stripslashes($content);
        }
        $pro['content'] = $content;
        $pro['beian'] = 1;
        $beian = I('post.beian', 0);
        if (!$beian) $pro['beian'] = 0;

        $pro['status'] = 0; //保存后修改状态
        $count = $project->save($pro);
        if ($count)
        {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("yewu/index"));
            $this->success("修改成功");
        } else
        {
            $this->error("修改失败或未修改");
        }

    }

    public function  shenhe()
    {
        $id = I('get.id');
        $project = M("project");
        $con["id"] = $id;
        $data["status"] = 1;
        if (!$project->where($con)->setField($data))
        {
            echo $project->getLastSql();
            exit;
            $this->error("审核失败");
        }
        $this->success("审核成功");
    }

    public function jindu()
    {
        $this->display();
    }

    public function del()
    {
        $project = M('project');
        $id = $_GET['id'];
        $count = $project->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

}
